package com.hbt.orderfood.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hbt.orderfood.R;
import com.hbt.orderfood.db.DBAdapter;
import com.hbt.orderfood.entitys.User;
import com.hbt.orderfood.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.simple_user)
    RadioButton simpleUser;
    @BindView(R.id.register)
    TextView register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.login)
    public void onViewClicked() {
        if (simpleUser.isChecked()) {
            String username_str = username.getText().toString();
//            String password_str = password.getText().toString();
            User user = new DBAdapter(this).getUserByUsername(username_str);
            if (user == null) {
                Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
            } else {
                StringUtils.USERID = user.getId();
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        } else {
            Intent intent = new Intent(LoginActivity.this, ManagerActivity.class);
            startActivity(intent);
        }

    }

    @OnClick(R.id.register)
    public void clickregister() {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        startActivity(intent);
    }
}
